
#import "RatingView.h"

@implementation RatingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initGrayStart];
        [self initYellowStart];
        [self initRatingLabel];
    }
    return self;
}

- (void)initGrayStart
{
    _grayStart = [[NSMutableArray alloc] initWithCapacity:5];
    for (int index=0; index<5; index++) {
        UIImageView *gray = [[UIImageView alloc] initWithFrame:CGRectZero];
        gray.image = [UIImage imageNamed:@"gray"];
        [self addSubview:gray];
        [_grayStart addObject:gray];
    }
}

- (void)initYellowStart
{
    _baseView = [[UIView alloc] initWithFrame:CGRectZero];
    [self addSubview:_baseView];
    _baseView.clipsToBounds = YES;
    
    _yellowStart = [[NSMutableArray alloc] initWithCapacity:5];
    for (int index=0; index<5; index++) {
        UIImageView *yellow = [[UIImageView alloc] initWithFrame:CGRectZero];
        yellow.image = [UIImage imageNamed:@"yellow"];
        [_baseView addSubview:yellow];
        [_yellowStart addObject:yellow];
    }
}

- (void)initRatingLabel
{
    _ratingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _ratingLabel.textColor = [UIColor yellowColor];
    [self addSubview:_ratingLabel];
}


- (void)setRatingData:(CGFloat)ratingData
{
    _ratingData = ratingData;
    _ratingLabel.text = [NSString stringWithFormat:@"%.1f",_ratingData];
    [_ratingLabel sizeToFit];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    int width = 0;
    
    for (int index=0; index<5; index++)
    {
        UIView *grayView = _grayStart[index];
        UIView *yellowView = _yellowStart[index];
        if (_style == kSmallStyle)
        {
            grayView.frame = CGRectMake(width, 0, 15, 14);
            yellowView.frame = CGRectMake(width, 0, 15, 14);
            width+=15;
        }
        else
        {
            grayView.frame = CGRectMake(width, 0, 35, 33);
            yellowView.frame = CGRectMake(width, 0, 35, 33);
            width+=35;
        }
        
    }
    
    float baseViewWidth = 0;
    baseViewWidth = _ratingData/10*width;
    _ratingLabel.frame = CGRectMake(width, 0, 0, 0);
    
    if (_style == kSmallStyle) {
        _baseView.frame = CGRectMake(0, 0, baseViewWidth, 14);
        _ratingLabel.font = [UIFont systemFontOfSize:14];
    }else{
        _baseView.frame = CGRectMake(0, 0, baseViewWidth, 33);
        _ratingLabel.font = [UIFont systemFontOfSize:24];
    }
    
    
}

@end
